#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL ksm(LL a,LL k){LL res=1;while(k>0){if(k&1) res=res*a%mod;k>>=1;a=a*a%mod;}return res%mod;}
vector<LL>g[maxn];
LL dep[maxn],vis[maxn];
LL col[maxn],cnt=0;
void dfs(LL u,LL d){
    dep[u]=d;vis[u]=1;
    for(LL i=0;i<g[u].size();i++){
        LL v=g[u][i];
        if(!vis[v]) dfs(v,d+1);
        else if(vis[v]==1) col[++cnt]=dep[u]-dep[v]+1;
    }
    vis[u]=2;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++){
       LL to;cin>>to;
       g[i].push_back(to);
       g[to].push_back(i);
   }
   for(LL i=1;i<=n;i++){
       if(!dep[i]) dfs(i,1);
   }
   debug(cnt);
   LL sum=0;LL res=1;
   for(LL i=1;i<=cnt;i++){
      sum+=col[i];
      res=(res%mod*(ksm(2,col[i]%mod)%mod-2+mod))%mod;
   }
   res=(res%mod*ksm(2,n-sum)%mod)%mod;
   cout<<res<<"\n";
   return 0;
}
